package com.itc.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.common.core.domain.R;
import com.itc.common.core.validate.Add;
import com.itc.common.core.validate.Update;
import com.itc.system.api.domain.SysDictData;
import com.itc.system.domain.dto.SysDictDataDto;
import com.itc.system.service.SysDictDataService;
import com.itc.common.core.utils.StringUtils;
import com.itc.common.core.utils.poi.ExcelUtil;
import com.itc.common.log.annotation.Log;
import com.itc.common.log.enums.BusinessType;
import com.itc.common.security.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author itc
 */
@RestController
@RequestMapping("/dict/data")
@Api(value = "sysDictData", tags = "数据字典信息管理")
public class SysDictDataController
{
    @Autowired
    private SysDictDataService dictDataService;

    @RequiresPermissions("system:dict:list")
    @PostMapping("/page")
    @ApiOperation(value = "分页查询字典数据")
    public R<Page<SysDictData>> list(@RequestBody SysDictData dictData)
    {
        Page<SysDictData> page = new Page<>();
        page.setOptimizeCountSql(true);
        page.setSize(dictData.getSize());
        page.setCurrent(dictData.getCurrent());
        Page<SysDictData> resultPage = dictDataService.page(page,new LambdaQueryWrapper<>(dictData));
        return R.ok(resultPage);
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    @ApiOperation(value = "字典数据导出")
    public void export(HttpServletResponse response, @RequestBody SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.list(new LambdaQueryWrapper<>(dictData));
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/detail/{dictCode}")
    @ApiOperation(value = "查询字典数据详细")
    public R<SysDictData> getInfo(@PathVariable Long dictCode)
    {
        return R.ok(dictDataService.getById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation(value = "根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public R<List<SysDictData>> dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictDataService.getBaseMapper().selectList(
                new LambdaQueryWrapper<SysDictData>()
                .eq(SysDictData::getDictType,dictType)
        );
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<>();
        }
        return R.ok(data);
    }

    /**
     * 新增字典数据
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增字典数据")
    public R add(@Validated(Add.class) @RequestBody SysDictDataDto dict)
    {
        return dictDataService.dictDataAdd(dict);
    }

    /**
     * 修改保存字典数据
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation(value = "修改保存字典数据")
    public R edit(@Validated(Update.class) @RequestBody SysDictDataDto dict)
    {
        return dictDataService.dictDataUpdate(dict);
    }

    /**
     * 删除字典数据
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{dictCodes}")
    @ApiOperation(value = "修改保存字典数据")
    public R remove(@PathVariable List<Long> dictCodes)
    {
        return dictDataService.dictDataDelete(dictCodes);
    }
}
